import { Component, Inject, Vue } from 'vue-property-decorator';
import { VERSION } from '@/constants';
import LoginService from '@/account/login.service';
import AccountService from '@/account/account.service';
<%_ if (enableTranslation) { _%>
import TranslationService from '@/locale/translation.service';
<%_ } %>

@Component
export default class JhiNavbar extends Vue {
  @Inject('loginService')
  private loginService: () => LoginService;
  <%_ if (enableTranslation) { _%>
  @Inject('translationService') private translationService: () => TranslationService;
  <%_ } %>
  @Inject('accountService') private accountService: () => AccountService;
  public version = VERSION ? 'v' + VERSION : '';
  private currentLanguage = this.$store.getters.currentLanguage;
  private languages: any = this.$store.getters.languages;
  private hasAnyAuthorityValue = false;

  created() {
    <%_ if (enableTranslation) { _%>
    this.translationService().refreshTranslation(this.currentLanguage);
    <%_ } _%>
  }

  public subIsActive(input) {
    const paths = Array.isArray(input) ? input : [input]
    return paths.some(path => {
      return this.$route.path.indexOf(path) === 0 // current path starts with this path string
    })
  }


  <%_ if (enableTranslation) { _%>
  public changeLanguage(newLanguage: string) : void {
    this.translationService().refreshTranslation(newLanguage);
  }

  public isActiveLanguage(key: string) : boolean {
    return key === this.$store.getters.currentLanguage;
  }
  <%_ } _%>

  public logout() : void {
    <%_ if (authenticationType !== 'jwt') { _%>
    this.loginService().logout().then(response => {
      this.$store.commit('logout');
      this.$router.push('/', () => {});
      <%_ if (authenticationType === 'oauth2') { _%>
      const data = response.data;
      let logoutUrl = data.logoutUrl;
      // if Keycloak, uri has protocol/openid-connect/token
      if (logoutUrl.indexOf('/protocol') > -1) {
        logoutUrl = logoutUrl + '?redirect_uri=' + window.location.origin;
      } else {
        // Okta
        logoutUrl = logoutUrl + '?id_token_hint=' +
        data.idToken + '&post_logout_redirect_uri=' + window.location.origin;
      }
      window.location.href = logoutUrl;
      <%_ } _%>
    });
    <%_ } else { _%>
    localStorage.removeItem('<%=jhiPrefixDashed %>-authenticationToken');
    sessionStorage.removeItem('<%=jhiPrefixDashed %>-authenticationToken');
    this.$store.commit('logout');
    this.$router.push('/', () => {});
    <%_ } _%>
  }

  public openLogin() : void {
    <%_ if (authenticationType !== 'oauth2') { _%>
    this.loginService().openLogin((<any>this).$root);
    <%_ } else { %>
    this.loginService().login();
    <%_ } %>
  }

  public get authenticated(): boolean {
    return this.$store.getters.authenticated;
  }

  public hasAnyAuthority(authorities: any): boolean {
    this.accountService().hasAnyAuthorityAndCheckAuth(authorities).then(value => {
      this.hasAnyAuthorityValue = value;
    });
    return this.hasAnyAuthorityValue;
  }

  public get openAPIEnabled() : boolean {
    return (this.$store.getters.activeProfiles.indexOf('api-docs') > -1);
  }

  public get inProduction() : boolean {
    return (this.$store.getters.activeProfiles.indexOf('prod') > -1);
  }
}
